from django.forms import ModelForm
import models



from django.template.loader import render_to_string
import django.forms as forms


class SelectWithPop(forms.Select):
    def render(self, name, *args, **kwargs):
        html = super(SelectWithPop, self).render(name, *args, **kwargs)
        popupplus = render_to_string("popupplus.html", {'field': name})
        return html+popupplus

class MultipleSelectWithPop(forms.SelectMultiple):
    def render(self, name, *args, **kwargs):
        html = super(MultipleSelectWithPop, self).render(name, *args, **kwargs)
        popupplus = render_to_string("popupplus.html", {'field': name})
        return html+popupplus

 
class MyForm(ModelForm):
    def as_table(self):
        "Returns this form rendered as HTML <tr>s -- excluding the <table></table>."
        return self._html_output(u'<tr><th valign="top">%(label)s</th><td>%(errors)s%(field)s%(help_text)s</td></tr>', u'<tr><td colspan="2">%s</td></tr>', '</td></tr>', u'<br />%s', False)


class BookForm(MyForm):
    publisher = forms.ModelChoiceField(models.Publisher.objects.all().order_by('name'), widget=SelectWithPop)
    authors = forms.ModelMultipleChoiceField(models.Author.objects.all().order_by('name'), required=False, widget=MultipleSelectWithPop)
    class Meta:
        model = models.Book
        
class AuthorForm(ModelForm):
    class Meta:
        model = models.Author
        
class PublisherForm(ModelForm):
    class Meta:
        model = models.Publisher
        
        